package cn.com.duiba.millionaire.center.spider.spdierjob;

import cn.com.duiba.millionaire.center.spider.bean.XiaoMiDataBean;
import cn.com.duiba.millionaire.center.spider.bean.XiaoMiJsonBean;
import cn.com.duiba.millionaire.center.spider.common.PageConsumer;
import cn.com.duiba.millionaire.center.spider.dao.appinfo.XiaoMiLocalSpiderDao;
import cn.com.duiba.millionaire.center.spider.entity.SpiderAppInfoEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.List;

/**
 * 小米应用商城 http://app.mi.com/
 * Created by sunchangji on 2018/6/11.
 */
@Component
public class XiaoMiAppSpiderJob {
    private static Logger log = LoggerFactory.getLogger(XiaoMiAppSpiderJob.class);
    private static final String XM_DETAIL_PRE = "http://app.mi.com/details?id=";
    private static final String JSON_API_PRE = "http://app.mi.com/categotyAllListApi?";

    @Autowired
    private XiaoMiLocalSpiderDao xiaoMiLocalSpiderDao;

    public PageConsumer pageConsumer = page -> {
        String url = page.getUrl().get();
        if (url.contains(JSON_API_PRE)) {
            XiaoMiJsonBean xiaoMiJsonBean = null;
            try {
                xiaoMiJsonBean = page.getJson().toObject(XiaoMiJsonBean.class);

            } catch (Exception e) {
                log.info("小米市场爬虫失败", e);
            }
            if (xiaoMiJsonBean != null) {
                List<XiaoMiDataBean> dataBeans = xiaoMiJsonBean.getData();
                if (CollectionUtils.isNotEmpty(dataBeans)) {
                    List<String> targetUrl = getNextJsonPage(dataBeans, url);
                    page.addTargetRequests(targetUrl);
                }
            }
        } else if (url.contains(XM_DETAIL_PRE)) {
            Html html = page.getHtml();
            String appName = html.xpath("//div[@class='intro-titles']//h3//text()").get();
            if (StringUtils.isNotEmpty(appName)) {
                String company = html.xpath("//div[@class='intro-titles']//p[1]//text()").get();
                String category = html.xpath("//div[@class='intro-titles']//p[@class='special-font']").get();
                if (StringUtils.isNotEmpty(category)) {
                    category = StringUtils.substringBetween(category, "</b>", "<span").trim();
                }
                String updateTime = html.xpath("//div[@class='details']//ul[@class='cf']//li[6]//text()").get();
                String desc = html.xpath("//p[@class='pslide']//text()").get();
                SpiderAppInfoEntity entity = new SpiderAppInfoEntity();
                entity.setAppName(appName);
                entity.setCategory(category);
                entity.setCompany(company);
                entity.setUpdatedDate(updateTime);
                entity.setDescription(desc);
                entity.setEmail(url);
                xiaoMiLocalSpiderDao.inserAppInfo(entity);
            }

        }
    };

    private static final String SPILTER = "=";
    private List<String> getNextJsonPage(List<XiaoMiDataBean> dataBeans, String url) {
        List<String> targetUrl = new ArrayList<>(35);
        String tUrlPrex = url.substring(0, url.lastIndexOf(SPILTER) + 1);
        Integer nextPage = Integer.valueOf(url.substring(url.lastIndexOf(SPILTER) + 1)) + 1;
        targetUrl.add(tUrlPrex + nextPage);
        for (XiaoMiDataBean dataBean : dataBeans) {
            targetUrl.add(XM_DETAIL_PRE + dataBean.getPackageName());
        }
        return targetUrl;
    }
}
